//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "Factorial.h"
using namespace Cephei::QL::Math;
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Math::CFactorial::CFactorial (boost::shared_ptr<QuantLib::Factorial>& childNative, Object^ owner) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phFactorial = NULL;
#endif
	_ppFactorial = &childNative;
    
}
Cephei::QL::Math::CFactorial::CFactorial (QuantLib::Factorial& childNative, Object^ owner) 
{
#ifdef HANDLE
	_phFactorial = NULL;
#endif
	_ppFactorial = new boost::shared_ptr<QuantLib::Factorial> (&childNative);
    
    _FactorialOwner = owner;
    
}

Cephei::QL::Math::CFactorial::CFactorial (CFactorial^ copy) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phFactorial = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppFactorial = new boost::shared_ptr<QuantLib::Factorial> (copy->GetShared());
        
    }
}
Cephei::QL::Math::CFactorial::CFactorial (System::Type^ t) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phFactorial = NULL;
#endif
	if (!t->IsSubclassOf(CFactorial::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Math::CFactorial::CFactorial (QuantLib::Handle<QuantLib::Factorial>& childNative, Object^ owner)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phFactorial = &childNative;
	_ppFactorial = &static_cast<boost::shared_ptr<QuantLib::Factorial>>(childNative.currentLink());
    
    _FactorialOwner = owner;
}
Cephei::QL::Math::CFactorial::CFactorial (QuantLib::Handle<QuantLib::Factorial> childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phFactorial = &childNative;
	_ppFactorial = &static_cast<boost::shared_ptr<QuantLib::Factorial>>(childNative.currentLink());
    
}
#endif
#ifdef STRUCT
Cephei::QL::Math::CFactorial::CFactorial (QuantLib::Factorial childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phFactorial = NULL;
#endif
	_ppFactorial = new boost::shared_ptr<QuantLib::Factorial> (new QuantLib::Factorial (childNative));
    
}
#endif

Cephei::QL::Math::CFactorial::~CFactorial ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
        _pSpinlock = NULL;
    }
    if (_ppFactorial != NULL)
    {
	    delete _ppFactorial;
        _ppFactorial = NULL;
    }
}
Cephei::QL::Math::CFactorial::!CFactorial ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
    }
    if (_ppFactorial != NULL)
    {
	    delete _ppFactorial;
    }
}
QuantLib::Factorial& Cephei::QL::Math::CFactorial::GetReference ()
{
    if (_ppFactorial == NULL) throw gcnew NativeNullException ();
	return **_ppFactorial;
}
boost::shared_ptr<QuantLib::Factorial>& Cephei::QL::Math::CFactorial::GetShared ()
{
    if (_ppFactorial == NULL) throw gcnew NativeNullException ();
	return *_ppFactorial;
}
QuantLib::Factorial* Cephei::QL::Math::CFactorial::GetPointer ()
{
    if (_ppFactorial == NULL) throw gcnew NativeNullException ();
	return &**_ppFactorial;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::Factorial>& Cephei::QL::Math::CFactorial::GetHandle ()
{
	if (_phFactorial == NULL)
	{
		_phFactorial = new Handle<QuantLib::Factorial> (*_ppFactorial);
	}
	return *_phFactorial;
}
#endif
bool Cephei::QL::Math::CFactorial::HasNative () 
{
	return (_ppFactorial != NULL);
}

Double Cephei::QL::Math::CFactorial::Get (UInt32 n)
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        QuantLib::Natural _n = (QuantLib::Natural)ValueHelper::Convert (n);
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppFactorial)->get ( _n );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Math::CFactorial::Ln (UInt32 n)
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        QuantLib::Natural _n = (QuantLib::Natural)ValueHelper::Convert (n);
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppFactorial)->ln ( _n );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

